﻿#include "CanDeviceDriver.h"

#include <QCoreApplication>
#include <QLibrary>
#include <QDebug>

#include "windows.h"

#include "QtChCharset.h"


CanDeviceDriver::CanDeviceDriver()
{
}


CanDeviceDriver::~CanDeviceDriver()
{
	UnloadDLL();
}

QSharedPointer<CanDeviceDriver> CanDeviceDriver::instance()
{
	static QSharedPointer<CanDeviceDriver> instance(new CanDeviceDriver());
	return instance;
}

void CanDeviceDriver::UnloadDLL()
{
#if LoadType == 0
	if (mlib.isLoaded())
	{
		mlib.unload();
	}
#elif LoadType == 1
	if (handleDll)
	{
		::FreeLibrary((HMODULE)handleDll);
		handleDll = NULL;
	}
#endif
	// 重置所有函数指针
	this->VCI_OpenDevice = nullptr;
	this->VCI_CloseDevice = nullptr;
	this->VCI_InitCAN = nullptr;
	this->VCI_ReadBoardInfo = nullptr;
	this->VCI_SetReference = nullptr;
	this->VCI_GetReceiveNum = nullptr;
	this->VCI_ClearBuffer = nullptr;
	this->VCI_StartCAN = nullptr;
	this->VCI_ResetCAN = nullptr;
	this->VCI_Transmit = nullptr;
	this->VCI_Receive = nullptr;
}



bool CanDeviceDriver::LoadDLL(QString dllFilePath)
{
	if (dllFilePath.isEmpty())
	{
		QString dirPath = QCoreApplication::applicationDirPath();
		dllFilePath = dirPath + "/ControlCAN.dll";
		//dllFilePath = dirPath + "/ControlCAN_Guangcheng.dll";
		//dllFilePath = dirPath + "/ControlCAN_Chuangxin.dll";
	}

	//
	UnloadDLL();

#if LoadType == 0
	//
	mlib.setFileName(dllFilePath);
	//mlib.setLoadHints(QLibrary::PreventUnloadHint);

	if (!mlib.load())
	{
		qWarning() << tr("加载CAN驱动dll失败，错误：") << mlib.errorString() << tr(", 路径：") << dllFilePath;
		return false;
	}
	this->VCI_OpenDevice = (VCI_OpenDevice_Define)mlib.resolve("VCI_OpenDevice");
	if (!this->VCI_OpenDevice)
	{
		qWarning() << "获取函数VCI_OpenDevice失败, " << mlib.errorString();
		return false;
	}
	// 加载函数
	this->VCI_OpenDevice = (VCI_OpenDevice_Define)mlib.resolve("VCI_OpenDevice");
	if (!this->VCI_OpenDevice)
	{
		qWarning() << "获取函数VCI_OpenDevice失败, " << mlib.errorString();
		return false;
	}
	this->VCI_CloseDevice = (VCI_CloseDevice_Define)mlib.resolve("VCI_CloseDevice");
	if (!this->VCI_CloseDevice)
	{
		qWarning() << "获取函数VCI_CloseDevice失败, " << mlib.errorString();
		return false;
	}
	this->VCI_InitCAN = (VCI_InitCAN_Define)mlib.resolve("VCI_InitCAN");
	if (!this->VCI_InitCAN)
	{
		qWarning() << "获取函数VCI_InitCAN失败, " << mlib.errorString();
		return false;
	}
	this->VCI_ReadBoardInfo = (VCI_ReadBoardInfo_Define)mlib.resolve("VCI_ReadBoardInfo");
	if (!this->VCI_ReadBoardInfo)
	{
		qWarning() << "获取函数VCI_ReadBoardInfo失败, " << mlib.errorString();
		return false;
	}
	this->VCI_SetReference = (VCI_SetReference_Define)mlib.resolve("VCI_SetReference");
	if (!this->VCI_SetReference)
	{
		qWarning() << "获取函数VCI_SetReference失败, " << mlib.errorString();
		return false;
	}
	this->VCI_GetReceiveNum = (VCI_GetReceiveNum_Define)mlib.resolve("VCI_GetReceiveNum");
	if (!this->VCI_GetReceiveNum)
	{
		qWarning() << "获取函数VCI_GetReceiveNum失败, " << mlib.errorString();
		return false;
	}
	this->VCI_ClearBuffer = (VCI_ClearBuffer_Define)mlib.resolve("VCI_ClearBuffer");
	if (!this->VCI_ClearBuffer)
	{
		qWarning() << "获取函数VCI_ClearBuffer失败, " << mlib.errorString();
		return false;
	}
	this->VCI_StartCAN = (VCI_StartCAN_Define)mlib.resolve("VCI_StartCAN");
	if (!this->VCI_StartCAN)
	{
		qWarning() << "获取函数VCI_StartCAN失败, " << mlib.errorString();
		return false;
	}
	this->VCI_ResetCAN = (VCI_ResetCAN_Define)mlib.resolve("VCI_ResetCAN");
	if (!this->VCI_ResetCAN)
	{
		qWarning() << "获取函数VCI_ResetCAN失败, " << mlib.errorString();
		return false;
	}
	this->VCI_Transmit = (VCI_Transmit_Define)mlib.resolve("VCI_Transmit");
	if (!this->VCI_Transmit)
	{
		qWarning() << "获取函数VCI_Transmit失败, " << mlib.errorString();
		return false;
	}
	this->VCI_Receive = (VCI_Receive_Define)mlib.resolve("VCI_Receive");
	if (!this->VCI_Receive)
	{
		qWarning() << "获取函数VCI_Receive失败, " << mlib.errorString();
		return false;
	}
#elif LoadType == 1
	HMODULE hDll = ::LoadLibraryA(dllFilePath.toStdString().c_str());
	if (hDll == NULL)
	{
		qWarning() << tr("加载CAN驱动dll失败，错误：") << GetLastError() << tr(", 路径：") << dllFilePath;
		return false;
	}
	handleDll = hDll;

	this->VCI_OpenDevice = (VCI_OpenDevice_Define)::GetProcAddress(hDll, "VCI_OpenDevice");
	if (!this->VCI_OpenDevice)
	{
		qWarning() << "获取函数VCI_OpenDevice失败, ";
		return false;
	}
	this->VCI_CloseDevice = (VCI_CloseDevice_Define)::GetProcAddress(hDll, "VCI_CloseDevice");
	if (!this->VCI_CloseDevice)
	{
		qWarning() << "获取函数VCI_CloseDevice失败, ";
		return false;
	}
	this->VCI_InitCAN = (VCI_InitCAN_Define)::GetProcAddress(hDll, "VCI_InitCAN");
	if (!this->VCI_InitCAN)
	{
		qWarning() << "获取函数VCI_InitCAN失败, ";
		return false;
	}
	this->VCI_ReadBoardInfo = (VCI_ReadBoardInfo_Define)::GetProcAddress(hDll, "VCI_ReadBoardInfo");
	if (!this->VCI_ReadBoardInfo)
	{
		qWarning() << "获取函数VCI_ReadBoardInfo失败, ";
		return false;
	}
	this->VCI_SetReference = (VCI_SetReference_Define)::GetProcAddress(hDll, "VCI_SetReference");
	if (!this->VCI_SetReference)
	{
		qWarning() << "获取函数VCI_SetReference失败, ";
		return false;
	}
	this->VCI_GetReceiveNum = (VCI_GetReceiveNum_Define)::GetProcAddress(hDll, "VCI_GetReceiveNum");
	if (!this->VCI_GetReceiveNum)
	{
		qWarning() << "获取函数VCI_GetReceiveNum失败, ";
		return false;
	}
	this->VCI_ClearBuffer = (VCI_ClearBuffer_Define)::GetProcAddress(hDll, "VCI_ClearBuffer");
	if (!this->VCI_ClearBuffer)
	{
		qWarning() << "获取函数VCI_ClearBuffer失败, ";
		return false;
	}
	this->VCI_StartCAN = (VCI_StartCAN_Define)::GetProcAddress(hDll, "VCI_StartCAN");
	if (!this->VCI_StartCAN)
	{
		qWarning() << "获取函数VCI_StartCAN失败, ";
		return false;
	}
	this->VCI_ResetCAN = (VCI_ResetCAN_Define)::GetProcAddress(hDll, "VCI_ResetCAN");
	if (!this->VCI_ResetCAN)
	{
		qWarning() << "获取函数VCI_ResetCAN失败, ";
		return false;
	}
	this->VCI_Transmit = (VCI_Transmit_Define)::GetProcAddress(hDll, "VCI_Transmit");
	if (!this->VCI_Transmit)
	{
		qWarning() << "获取函数VCI_Transmit失败, ";
		return false;
	}
	this->VCI_Receive = (VCI_Receive_Define)::GetProcAddress(hDll, "VCI_Receive");
	if (!this->VCI_Receive)
	{
		qWarning() << "获取函数VCI_Receive失败, ";
		return false;
	}
#elif LoadType == 2
	this->VCI_OpenDevice = &::VCI_OpenDevice;
	if (!this->VCI_OpenDevice)
	{
		qWarning() << "获取函数VCI_OpenDevice失败, ";
		return false;
	}
	this->VCI_CloseDevice = &::VCI_CloseDevice;
	if (!this->VCI_CloseDevice)
	{
		qWarning() << "获取函数VCI_CloseDevice失败, ";
		return false;
	}
	this->VCI_InitCAN = &::VCI_InitCAN;
	if (!this->VCI_InitCAN)
	{
		qWarning() << "获取函数VCI_InitCAN失败, ";
		return false;
	}
	this->VCI_ReadBoardInfo = &::VCI_ReadBoardInfo;
	if (!this->VCI_ReadBoardInfo)
	{
		qWarning() << "获取函数VCI_ReadBoardInfo失败, ";
		return false;
	}
	this->VCI_SetReference = &::VCI_SetReference;
	if (!this->VCI_SetReference)
	{
		qWarning() << "获取函数VCI_SetReference失败, ";
		return false;
	}
	this->VCI_GetReceiveNum = &::VCI_GetReceiveNum;
	if (!this->VCI_GetReceiveNum)
	{
		qWarning() << "获取函数VCI_GetReceiveNum失败, ";
		return false;
	}
	this->VCI_ClearBuffer = &::VCI_ClearBuffer;
	if (!this->VCI_ClearBuffer)
	{
		qWarning() << "获取函数VCI_ClearBuffer失败, ";
		return false;
	}
	this->VCI_StartCAN = &::VCI_StartCAN;
	if (!this->VCI_StartCAN)
	{
		qWarning() << "获取函数VCI_StartCAN失败, ";
		return false;
	}
	this->VCI_ResetCAN = &::VCI_ResetCAN;
	if (!this->VCI_ResetCAN)
	{
		qWarning() << "获取函数VCI_ResetCAN失败, ";
		return false;
	}
	this->VCI_Transmit = &::VCI_Transmit;
	if (!this->VCI_Transmit)
	{
		qWarning() << "获取函数VCI_Transmit失败, ";
		return false;
	}
	this->VCI_Receive = &::VCI_Receive;
	if (!this->VCI_Receive)
	{
		qWarning() << "获取函数VCI_Receive失败, ";
		return false;
	}
#endif

	return true;
}
